package br.com.vti.sisp_sicrim.antigo.controller.filter;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import br.com.caelum.vraptor.InterceptionException;
import br.com.caelum.vraptor.Intercepts;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.core.InterceptorStack;
import br.com.caelum.vraptor.interceptor.Interceptor;
import br.com.caelum.vraptor.ioc.RequestScoped;
import br.com.caelum.vraptor.resource.ResourceMethod;

@Intercepts
@RequestScoped
public class ResultInterceptor implements Interceptor {

    private final Result result;

    public ResultInterceptor(Result result) {
        this.result = result;
    }

    private String obterDataCorrente() {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        return sdf.format(Calendar.getInstance().getTime());
    }

    private String obterHoraCorrente() {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        return sdf.format(Calendar.getInstance().getTime());
    }

    public void intercept(InterceptorStack stack, ResourceMethod method, Object instance) throws InterceptionException {
        result.include("versao", "1.0");
        result.include("dataCorrente", obterDataCorrente());
        result.include("horaCorrente", obterHoraCorrente());
        result.include("empresa", "SSP");
        result.include("programa", instance.getClass().getSimpleName().toUpperCase(Locale.getDefault()));
        stack.next(method, instance);
    }

    public boolean accepts(ResourceMethod method) {
        return true;
    }

}
